package com.controller.common;

import com.model.base.Result;
import com.common.anntation.Log;
import com.common.emums.BusinessType;

import com.common.utils.EntityConversionUtil;
import com.core.springSecurity.model.TerminalManal;
import com.core.springSecurity.service.PermissionService;
import com.core.springSecurity.service.TerminalService;
import com.model.vo.sys.common.TerminalManalVO;
import com.model.vo.sys.common.TerminalVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zql
 * @Description: 终端管理接口
 * @Data: 14:01 2021/7/23
 */
@Api(tags = {"终端管理"})
@Slf4j
@RestController
@RequestMapping("/v1")
public class TerminalController {

    @Resource
    TerminalService terminalService;

    @ApiOperation(value = "获取在线用户")
    @Log(title = "获取在线用户" ,businessType = BusinessType.SELECT,isSaveRequestData=true)
    @PostMapping("/getTerminalList")
    @PreAuthorize("@ss.hasAnyPermi('" + PermissionService.SYSTEM_OPERATE + "')")
    public Result<TerminalManalVO> getTerminalList() {
        TerminalManal terminalManal = terminalService.getTerminal();
        List<TerminalVO> terminalList =  EntityConversionUtil.copyList(new ArrayList<>(terminalManal.getMap().values()),TerminalVO.class);
        return Result.suc(TerminalManalVO.builder().loginCount(terminalManal.getLoginCount()).terminalList(terminalList).build());
    }

    @ApiOperation(value = "强制注销用户")
    @Log(title = "强制注销用户" ,businessType = BusinessType.SELECT,isSaveRequestData=true)
    @GetMapping("/forcedLogoutUser")
    @PreAuthorize("@ss.hasAnyPermi('" + PermissionService.SYSTEM_OPERATE + "')")
    public Result<Object> forcedLogoutUser(String loginKey) {
         terminalService.forcedLogoutUser(loginKey);
        return Result.suc();
    }
}
